home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Drivers / powerglove.lha / Fingers / pwrglove.c < prev    next >
C/C++ Source or Header  |  1991-10-13  |  13KB  |  823 lines

  1. #include "pwrglove.h"
  2. #include <exec/types.h>
  3. #include <exec/ports.h>
  4. #include <exec/memory.h>
  5. #include <exec/interrupts.h>
  6. #include <hardware/cia.h>
  7. #include <resources/cia.h>
  8.  
  9. #include <proto/exec.h>
  10.  
  11. static volatile int times_up;
  12. extern struct CIA ciab;
  13. static struct Library *CIAResource;
  14. static struct Interrupt interrupt;
  15. static UBYTE interrupting;
  16.  
  17. void closetimer(void);
  18.  
  19. void
  20. fatal(char *s)
  21. {
  22.     printf("fatal error: %s\n", s);
  23.     closetimer();
  24.     exit(1);
  25. }
  26.  
  27. void
  28. timerhandler()
  29. {
  30.     /* hardware timer interrupt */
  31.     times_up = 1;
  32. }
  33.  
  34. void
  35. timersleep(long ticks)
  36. {
  37.     times_up = 0;
  38.  
  39.     ticks -= 8; if (ticks <= 0) { times_up=1; return; } /* yucko */
  40.  
  41.     /* 1.397 usec * n */
  42.     ciab.ciatblo = ticks & 0xff;
  43.     ciab.ciatbhi = (ticks>>8) & 0xff;
  44.  
  45.     /* one-shot */
  46.     ciab.ciacrb |= CIACRBF_RUNMODE | CIACRBF_LOAD | CIACRBF_START;
  47.  
  48.     while (!times_up) {}
  49. }
  50.  
  51. void
  52. opentimer()
  53. {
  54.     static char* timername = "glove.timer";
  55.  
  56.     /* install interrupt handler */
  57.     CIAResource = OpenResource(CIABNAME);
  58.     if (CIAResource == NULL) fatal("can't get timer resource");
  59.  
  60.     interrupt.is_Node.ln_Type = NT_INTERRUPT;
  61.     interrupt.is_Node.ln_Pri = 127;
  62.     interrupt.is_Node.ln_Name = timername;
  63.     interrupt.is_Data = NULL;
  64.     interrupt.is_Code = timerhandler;
  65.     if (AddICRVector(CIAResource, CIAICRB_TB, &interrupt) != NULL) {
  66.         fatal("can't use CIA B Timer B");
  67.     }
  68.  
  69.     /* this timer is all mine */
  70.     interrupting = 1;
  71.  
  72.     SetICR(CIAResource, CIAICRF_TB);
  73.     AbleICR(CIAResource, CIAICRF_SETCLR | CIAICRF_TB);
  74.     times_up = 0;
  75. }
  76.  
  77. void
  78. closetimer()
  79. {
  80.     if (interrupting) {
  81.         ciab.ciacrb &= ~CIACRBF_START;
  82.         AbleICR(CIAResource, CIAICRF_TB);
  83.         RemICRVector(CIAResource, CIAICRB_TB, &interrupt);
  84.         interrupting = 0;
  85.     }
  86. }
  87.  
  88. extern struct CIA far ciaa;
  89.  
  90. /* bits from parallel port -- Alan's hack */
  91. #define        GDATA           0x04    /* glove data in */
  92. #define        GLATCH          0x02    /* glove latch out */
  93. #define        GCLOCK          0x01    /* glove clock out */
  94. #define        GCLOLAT         (GLATCH|GCLOCK) /* latch and clock */
  95.  
  96. #define getbit()      (ciaa.ciaprb & GDATA) >> 2
  97. #define initport()    ciaa.ciaddrb = GCLOLAT
  98.  
  99. /* delays in microseconds */
  100. #define     D2BYTES      96
  101. #define     D2BITS       22
  102. #define     D2SLOW       14720
  103.  
  104. #define     C0L0()       ciaa.ciaprb = 0        /* clock 0 latch 0 */
  105. #define     C0L1()       ciaa.ciaprb = GLATCH   /* clock 0 latch 1 */
  106. #define     C1L0()       ciaa.ciaprb = GCLOCK   /* clock 1 latch 0 */
  107. #define     C1L1()       ciaa.ciaprb = GCLOLAT  /* clock 1 latch 1 */
  108.  
  109. #define     setporta()    delay(3)
  110. #define     setportb()  delay(3)
  111.  
  112. unsigned char getbyte (void);
  113.  
  114. /* convert microseconds to cia ticks */
  115. #define delay(usec) timersleep((usec*1397)/1000)
  116.  
  117. int control_c()
  118. {
  119.     closetimer();
  120.     printf("<<goodbye>>\n");
  121.     return 1; /* causes exit */
  122. }
  123.  
  124. void query_glove(glove_data *g)
  125. {
  126.     unsigned char buf[12];
  127.     register char *bp;
  128.  
  129.     /* read 12 byte packet */
  130.     bp = buf;
  131.     *bp++ = getbyte ();
  132.     delay (D2BYTES);
  133.     *bp++ = getbyte ();
  134.     delay (D2BYTES);
  135.     *bp++ = getbyte ();
  136.     delay (D2BYTES);
  137.     *bp++ = getbyte ();
  138.     delay (D2BYTES);
  139.     *bp++ = getbyte ();
  140.     delay (D2BYTES);
  141.     *bp++ = getbyte ();
  142.     delay (D2BYTES);
  143.     *bp++ = getbyte ();
  144.     delay (D2BYTES);
  145.     *bp++ = getbyte ();
  146.     delay (D2SLOW);
  147.     *bp++ = getbyte ();
  148.     delay (D2SLOW);
  149.     *bp++ = getbyte ();
  150.     delay (D2SLOW);
  151.     *bp++ = getbyte ();
  152.     delay (D2SLOW);
  153.     *bp++ = getbyte ();
  154.     delay (D2SLOW);
  155.  
  156.     /* Glove packet isn't quite as described above */
  157.     /* Let's see if we can figure it out */
  158.     {
  159.         int i,k,n;
  160.  
  161.         /* look for FF FF A0 */
  162.         /* almost always starts at offset 7 so look there first */
  163.         n = -1;
  164.         for (k=0,i=7; k<12; ++k,++i) {
  165.             if (buf[i%12] == (unsigned char)0xff &&
  166.                 buf[(i+1)%12] == (unsigned char)0xff &&
  167.                 buf[(i+2)%12] == (unsigned char)0xa0) {
  168.  
  169.                 /* yah! */
  170.                 n = (i+3)%12;
  171.                 g->x = buf[n];
  172.                 g->y = buf[(n+1)%12];
  173.                 g->z = buf[(n+2)%12];
  174.                 g->rot = buf[(n+3)%12];
  175.                 g->fingers = buf[(n+4)%12];
  176.                 g->keys = buf[(n+5)%12];
  177.                 g->dum0 = 0xa0;
  178.                 g->dum7 = buf[(n+6)%12];
  179.                 g->dum8 = buf[(n+7)%12];
  180.                 g->dum9 = buf[(n+8)%12];
  181.                 g->dumA = 0xff;
  182.                 g->dumB = 0xff;
  183.                 return;
  184.             }
  185.         }
  186.         /* no valid data found */
  187.     }
  188. }
  189.  
  190. unsigned char getbyte ()
  191. {
  192.     register unsigned Glov = 0;    
  193.  
  194.     /* prepare port b as output port */
  195.     setportb ();
  196.  
  197.         /* generate a reset (latch) pulse */
  198.     C1L0 ();
  199.     C1L1 ();
  200.     delay(5); /* 5 us delay */
  201.     C1L0 ();
  202.  
  203.     /* configure port a as input */
  204.     setporta ();
  205.  
  206.     /* read a bit */
  207.     Glov <<= 1;
  208.     Glov |= getbit();
  209.  
  210.     /* prepare port b as output port */
  211.     setportb ();
  212.  
  213.     /* generate a clock pulse */
  214.     C0L0 ();
  215.     C1L0 ();
  216.     
  217.     /* configure port a as input */
  218.     setporta ();
  219.  
  220.     /* read a bit */
  221.     Glov <<= 1;
  222.     Glov |= getbit();
  223.  
  224.     /* prepare port b as output port */
  225.     setportb ();
  226.  
  227.     /* generate a clock pulse */
  228.     C0L0 ();
  229.     C1L0 ();
  230.     
  231.     /* configure port a as input */
  232.     setporta ();
  233.  
  234.     /* read a bit */
  235.     Glov <<= 1;
  236.     Glov |= getbit();
  237.  
  238.     /* prepare port b as output port */
  239.     setportb ();
  240.  
  241.     /* generate a clock pulse */
  242.     C0L0 ();
  243.     C1L0 ();
  244.     
  245.     /* configure port a as input */
  246.     setporta ();
  247.  
  248.     /* read a bit */
  249.     Glov <<= 1;
  250.     Glov |= getbit();
  251.  
  252.     /* prepare port b as output port */
  253.     setportb ();
  254.  
  255.     /* generate a clock pulse */
  256.     C0L0 ();
  257.     C1L0 ();
  258.     
  259.     /* configure port a as input */
  260.     setporta ();
  261.  
  262.     /* read a bit */
  263.     Glov <<= 1;
  264.     Glov |= getbit();
  265.  
  266.     /* prepare port b as output port */
  267.     setportb ();
  268.  
  269.     /* generate a clock pulse */
  270.     C0L0 ();
  271.     C1L0 ();
  272.     
  273.     /* configure port a as input */
  274.     setporta ();
  275.  
  276.     /* read a bit */
  277.     Glov <<= 1;
  278.     Glov |= getbit();
  279.  
  280.     /* prepare port b as output port */
  281.     setportb ();
  282.  
  283.     /* generate a clock pulse */
  284.     C0L0 ();
  285.     C1L0 ();
  286.     
  287.     /* configure port a as input */
  288.     setporta ();
  289.  
  290.     /* read a bit */
  291.     Glov <<= 1;
  292.     Glov |= getbit();
  293.  
  294.     /* prepare port b as output port */
  295.     setportb ();
  296.  
  297.     /* generate a clock pulse */
  298.     C0L0 ();
  299.     C1L0 ();
  300.     
  301.     /* configure port a as input */
  302.     setporta ();
  303.  
  304.     /* read a bit */
  305.     Glov <<= 1;
  306.     Glov |= getbit();
  307.  
  308.     /* prepare port b as output port */
  309.     setportb ();
  310.  
  311.     /* generate a clock pulse */
  312.     C0L0 ();
  313.     C1L0 ();
  314.  
  315.     return (unsigned char) Glov;  /* return the byte */
  316. }
  317.  
  318.  
  319. void init_glove()
  320. {
  321.     register unsigned char Glov = 0;
  322.  
  323.     opentimer();
  324.     onbreak(control_c);
  325.  
  326.     /* initialize hardware interface */
  327.     initport();
  328.  
  329.     /* read 4 bits from glove */
  330.     setportb ();
  331.  
  332.         /* generate a reset (latch) pulse */
  333.     C1L0 ();
  334.     C1L1 ();
  335.     delay(5);  /* 5 us delay */
  336.     C1L0 ();
  337.  
  338.     /* configure port a as input */
  339.     setporta ();
  340.  
  341.     /* read a bit */
  342.     Glov <<= 1;
  343.     Glov |= getbit();
  344.  
  345.     /* prepare port b as output port */
  346.     setportb ();
  347.  
  348.     /* generate a clock pulse */
  349.     C0L0 ();
  350.     C1L0 ();
  351.     
  352.     /* configure port a as input */
  353.     setporta ();
  354.  
  355.     /* read a bit */
  356.     Glov <<= 1;
  357.     Glov |= getbit();
  358.  
  359.     /* prepare port b as output port */
  360.     setportb ();
  361.  
  362.     /* generate a clock pulse */
  363.     C0L0 ();
  364.     C1L0 ();
  365.     
  366.     /* configure port a as input */
  367.     setporta ();
  368.  
  369.     /* read a bit */
  370.     Glov <<= 1;
  371.     Glov |= getbit();
  372.  
  373.     /* prepare port b as output port */
  374.     setportb ();
  375.  
  376.     /* generate a clock pulse */
  377.     C0L0 ();
  378.     C1L0 ();
  379.     
  380.     /* configure port a as input */
  381.     setporta ();
  382.  
  383.     /* read a bit */
  384.     Glov <<= 1;
  385.     Glov |= getbit();
  386.  
  387.     /* prepare port b as output port */
  388.     setportb ();
  389.  
  390.     /* generate a clock pulse */
  391.     C0L0 ();
  392.     C1L0 ();
  393.     
  394.     /* end of read 4 bits */
  395.  
  396.     /* prepare port b as output port */
  397.     setportb ();
  398.  
  399.     C1L0 ();
  400.     delay(7212);
  401. /*    delay (16950);  /* 7212 us delay */
  402.  
  403.     setportb ();
  404.  
  405.     C1L1 ();
  406.     delay(2260);
  407. /*    delay (4750);   /* 2260 us delay */
  408.  
  409.     /* prepare port b as output port */
  410.     setportb ();
  411.  
  412.     C1L0 ();    /* Start of 1. Byte */
  413.     C0L0 ();
  414.     C1L0 ();
  415.     delay (D2BITS);
  416.  
  417.     /* prepare port b as output port */
  418.     setportb ();
  419.  
  420.     C0L0 ();
  421.     C1L0 ();
  422.     delay (D2BITS);
  423.     /* prepare port b as output port */
  424.     setportb ();
  425.  
  426.     C0L0 ();
  427.     C1L0 ();
  428.     delay (D2BITS);    
  429.  
  430.     /* prepare port b as output port */
  431.     setportb ();
  432.  
  433.     C0L0 ();
  434.     C1L0 ();
  435.     delay (D2BITS);
  436.  
  437.     /* prepare port b as output port */
  438.     setportb ();
  439.  
  440.     C0L0 ();
  441.     C1L0 ();
  442.     delay (D2BITS);
  443.  
  444.     /* prepare port b as output port */
  445.     setportb ();
  446.  
  447.     C1L1 ();
  448.     C0L1 ();
  449.     C1L1 ();
  450.     delay (D2BITS);
  451.     
  452.     /* prepare port b as output port */
  453.     setportb ();
  454.  
  455.     C0L1 ();
  456.     C1L1 ();
  457.     delay (D2BITS);    
  458.  
  459.     /* prepare port b as output port */
  460.     setportb ();
  461.  
  462.     C1L0 ();
  463.     C0L0 ();
  464.     C1L0 ();
  465.     delay (D2BYTES);        
  466.  
  467.     /* prepare port b as output port */
  468.     setportb ();
  469.  
  470.     C1L1 ();    /* Start of 2. Byte */
  471.     C0L1 ();
  472.     C1L1 ();
  473.     delay (D2BITS);
  474.  
  475.     /* prepare port b as output port */
  476.     setportb ();
  477.  
  478.     C0L1 ();
  479.     C1L1 ();
  480.     delay (D2BITS);
  481.  
  482.     /* prepare port b as output port */
  483.     setportb ();
  484.  
  485.     C1L0 ();
  486.     C0L0 ();
  487.     C1L0 ();
  488.     delay (D2BITS);
  489.  
  490.     /* prepare port b as output port */
  491.     setportb ();
  492.  
  493.     C0L0 ();
  494.     C1L0 ();
  495.     delay (D2BITS);
  496.  
  497.     /* prepare port b as output port */
  498.     setportb ();
  499.  
  500.     C0L0 ();
  501.     C1L0 ();
  502.     delay (D2BITS);
  503.  
  504.     /* prepare port b as output port */
  505.     setportb ();
  506.  
  507.     C0L0 ();
  508.     C1L0 ();
  509.     delay (D2BITS);
  510.  
  511.     /* prepare port b as output port */
  512.     setportb ();
  513.  
  514.     C0L0 ();
  515.     C1L0 ();
  516.     delay (D2BITS);
  517.  
  518.     /* prepare port b as output port */
  519.     setportb ();
  520.  
  521.     C1L1 ();
  522.     C0L1 ();
  523.     C1L1 ();
  524.     delay (D2BYTES);
  525.  
  526.     /* prepare port b as output port */
  527.     setportb ();
  528.  
  529.     C1L0 ();    /* Start of 3. Byte */
  530.     C0L0 ();
  531.     C1L0 ();
  532.     delay (D2BITS);
  533.  
  534.     /* prepare port b as output port */
  535.     setportb ();
  536.  
  537.     C0L0 ();
  538.     C1L0 ();
  539.     delay (D2BITS);    
  540.  
  541.     /* prepare port b as output port */
  542.     setportb ();
  543.  
  544.     C0L0 ();
  545.     C1L0 ();
  546.     delay (D2BITS);    
  547.  
  548.     /* prepare port b as output port */
  549.     setportb ();
  550.  
  551.     C0L0 ();
  552.     C1L0 ();
  553.     delay (D2BITS);    
  554.  
  555.     /* prepare port b as output port */
  556.     setportb ();
  557.  
  558.     C1L1 ();
  559.     C0L1 ();
  560.     C1L1 ();
  561.     delay (D2BITS);
  562.  
  563.     /* prepare port b as output port */
  564.     setportb ();
  565.  
  566.     C1L0 ();
  567.     C0L0 ();
  568.     C1L0 ();
  569.     delay (D2BITS);
  570.  
  571.     /* prepare port b as output port */
  572.     setportb ();
  573.  
  574.     C0L0 ();
  575.     C1L0 ();
  576.     delay (D2BITS);
  577.  
  578.     /* prepare port b as output port */
  579.     setportb ();
  580.  
  581.     C0L0 ();
  582.     C1L0 ();
  583.     delay (D2BYTES);
  584.  
  585.     /* prepare port b as output port */
  586.     setportb ();
  587.  
  588.     C0L0 ();    /* start of 4. byte */
  589.     C1L0 ();
  590.     delay (D2BITS);
  591.  
  592.     /* prepare port b as output port */
  593.     setportb ();
  594.  
  595.     C0L0 ();
  596.     C1L0 ();
  597.     delay (D2BITS);
  598.  
  599.     /* prepare port b as output port */
  600.     setportb ();
  601.  
  602.     C0L0 ();
  603.     C1L0 ();
  604.     delay (D2BITS);
  605.  
  606.     /* prepare port b as output port */
  607.     setportb ();
  608.  
  609.     C0L0 ();
  610.     C1L0 ();
  611.     delay (D2BITS);
  612.  
  613.     /* prepare port b as output port */
  614.     setportb ();
  615.  
  616.     C0L0 ();
  617.     C1L0 ();
  618.     delay (D2BITS);
  619.  
  620.     /* prepare port b as output port */
  621.     setportb ();
  622.  
  623.     C0L0 ();
  624.     C1L0 ();
  625.     delay (D2BITS);
  626.  
  627.     /* prepare port b as output port */
  628.     setportb ();
  629.  
  630.     C0L0 ();
  631.     C1L0 ();
  632.     delay (D2BITS);
  633.  
  634.     /* prepare port b as output port */
  635.     setportb ();
  636.  
  637.     C0L0 ();
  638.     C1L0 ();
  639.     delay (D2BYTES);
  640.  
  641.     /* prepare port b as output port */
  642.     setportb ();
  643.  
  644.     C0L0 ();    /* start of 5. byte */
  645.     C1L0 ();
  646.     delay (D2BITS);
  647.  
  648.     /* prepare port b as output port */
  649.     setportb ();
  650.  
  651.     C0L0 ();
  652.     C1L0 ();
  653.     delay (D2BITS);
  654.  
  655.     /* prepare port b as output port */
  656.     setportb ();
  657.  
  658.     C0L0 ();
  659.     C1L0 ();
  660.     delay (D2BITS);
  661.  
  662.     /* prepare port b as output port */
  663.     setportb ();
  664.  
  665.     C0L0 ();
  666.     C1L0 ();
  667.     delay (D2BITS);
  668.  
  669.     /* prepare port b as output port */
  670.     setportb ();
  671.  
  672.     C0L0 ();
  673.     C1L0 ();
  674.     delay (D2BITS);
  675.  
  676.     /* prepare port b as output port */
  677.     setportb ();
  678.  
  679.     C0L0 ();
  680.     C1L0 ();
  681.     delay (D2BITS);
  682.  
  683.     /* prepare port b as output port */
  684.     setportb ();
  685.  
  686.     C1L1 ();
  687.     C0L1 ();
  688.     C1L1 ();
  689.     delay (D2BITS);
  690.  
  691.     /* prepare port b as output port */
  692.     setportb ();
  693.  
  694.     C1L0 ();
  695.     C0L0 ();
  696.     C1L0 ();
  697.     delay (D2BYTES);
  698.  
  699.     /* prepare port b as output port */
  700.     setportb ();
  701.  
  702.     C1L1 ();    /* start of 6. byte */
  703.     C0L1 ();
  704.     C1L1 ();
  705.     delay (D2BITS);
  706.  
  707.     /* prepare port b as output port */
  708.     setportb ();
  709.  
  710.     C0L1 ();
  711.     C1L1 ();
  712.     delay (D2BITS);
  713.  
  714.     /* prepare port b as output port */
  715.     setportb ();
  716.  
  717.     C0L1 ();
  718.     C1L1 ();
  719.     delay (D2BITS);
  720.  
  721.     /* prepare port b as output port */
  722.     setportb ();
  723.  
  724.     C0L1 ();
  725.     C1L1 ();
  726.     delay (D2BITS);
  727.  
  728.     /* prepare port b as output port */
  729.     setportb ();
  730.  
  731.     C0L1 ();
  732.     C1L1 ();
  733.     delay (D2BITS);
  734.  
  735.     /* prepare port b as output port */
  736.     setportb ();
  737.  
  738.     C0L1 ();
  739.     C1L1 ();
  740.     delay (D2BITS);
  741.  
  742.     /* prepare port b as output port */
  743.     setportb ();
  744.  
  745.     C0L1 ();
  746.     C1L1 ();
  747.     delay (D2BITS);
  748.  
  749.     /* prepare port b as output port */
  750.     setportb ();
  751.  
  752.     C0L1 ();
  753.     C1L1 ();
  754.     delay (D2BYTES);
  755.  
  756.     /* prepare port b as output port */
  757.     setportb ();
  758.  
  759.     C1L0 ();    /* start of 7. byte */
  760.     C0L0 ();
  761.     C1L0 ();
  762.     delay (D2BITS);
  763.  
  764.     /* prepare port b as output port */
  765.     setportb ();
  766.  
  767.     C0L0 ();
  768.     C1L0 ();
  769.     delay (D2BITS);
  770.  
  771.     /* prepare port b as output port */
  772.     setportb ();
  773.  
  774.     C0L0 ();
  775.     C1L0 ();
  776.     delay (D2BITS);
  777.  
  778.     /* prepare port b as output port */
  779.     setportb ();
  780.  
  781.     C0L0 ();
  782.     C1L0 ();
  783.     delay (D2BITS);
  784.  
  785.     /* prepare port b as output port */
  786.     setportb ();
  787.  
  788.     C0L0 ();
  789.     C1L0 ();
  790.     delay (D2BITS);
  791.  
  792.     /* prepare port b as output port */
  793.     setportb ();
  794.  
  795.     C0L0 ();
  796.     C1L0 ();
  797.     delay (D2BITS);
  798.  
  799.     /* prepare port b as output port */
  800.     setportb ();
  801.  
  802.     C0L0 ();
  803.     C1L0 ();
  804.     delay (D2BITS);
  805.  
  806.     /* prepare port b as output port */
  807.     setportb ();
  808.  
  809.     C1L1 ();
  810.     C0L1 ();
  811.     C1L1 ();
  812.     delay(892);
  813. /*    delay (1090); /* 892 us delay (end of 7. byte) */
  814.  
  815.     /* prepare port b as output port */
  816.     setportb ();
  817.  
  818.     C1L0 ();
  819.     delay(50000);
  820. /*    delay (60000);   /* some time for the glove controller
  821.                         to relax */
  822. }
  823.